import logging.config
import os

root_path = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))
log_file_path = os.path.join(root_path, 'log')
log_file = os.path.join(log_file_path, 'log.txt')
if not os.path.exists(log_file_path):
    os.makedirs(log_file_path)
    file = open(log_file, 'a+')
    file.close()
    

LOGGING_CONFIG = {
    "version": 1,
    "formatters": {
        "default": {
            'format': '%(asctime)s %(filename)s %(lineno)s %(levelname)s %(message)s',
        },
        "plain": {
            "format": "%(message)s",
        },
    },
    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "level": logging.DEBUG,
            "formatter": "default",
        },
        "console_plain": {
            "class": "logging.StreamHandler",
            "level": logging.DEBUG,
            "formatter": "plain"
        },
        "file": {
            "class": "logging.handlers.RotatingFileHandler",
            "level": logging.DEBUG,
            "filename": log_file,
            'maxBytes': 100000000,  # 日志文件最大大小，单位是字节
            'backupCount': 10,  # 最多保留的备份日志文件个数
            "formatter": "default",
        }
    },
    "loggers": {
        "console_logger": {
            "handlers": ["console"],
            "level": "DEBUG",
            "propagate": False,
        },
        "console_plain_logger": {
            "handlers": ["console_plain"],
            "level": "DEBUG",
            "propagate": False,
        },
        "file_logger": {
            "handlers": ["file"],
            "level": "DEBUG",
            "propagate": False,
        },
        "mixed_logger": {
            "handlers": ["file","console"],
            "level": "DEBUG",
            "propagate": False,
        }
    },
    
    "disable_existing_loggers": True,
}

logging.config.dictConfig(LOGGING_CONFIG)
logger = logging.getLogger("mixed_logger")
# logger = logging.getLogger("console_logger")
